import LocalStorageManager from "./LocalStorageManager";
import NotifyMrg, { NotifyEventType } from "./NotifyMrg";
import PropConfig, { PropInfo, PropType } from "./PropConfig";
import Singleton from "./SingleTon";

export default class PropManager extends Singleton {
    /**
     * 道具数量
     */
    private _propData: Map<number, any> = new Map<number, any>();

    public init() {
        let propInfo = LocalStorageManager.getItem("propNum");
        if (!propInfo) {
            // 初始化本地存储道具数量
            let props = {};
            for (const prop of PropConfig.getPropCfg()) {
                props[prop.id] = 0;
                this._propData.set(prop.id, 0);
            }
            LocalStorageManager.setItem("propNum", props);
        } else {
            for (const id in propInfo) {
                this._propData.set(parseInt(id), propInfo[id]);
            }
        }
    }

    public updatePropNumInfo() {
        let propInfo = {};
        this._propData.forEach((value, key) => {
            propInfo[key] = value;
        });

        LocalStorageManager.setItem("propNum", propInfo);
        NotifyMrg.getInstance().sendNotify(NotifyEventType.PropNum);
    }

    /**
     * 获取道具数量
     */
    public getPropNum(propId: number) {
        return this._propData.get(propId);
    }

    /**
     * 设置道具数量
     */
    public setPropNum(propId: number, num: number) {
        this._propData.set(propId, num);
        this.updatePropNumInfo();
    }

    /**获取道具信息 */
    public getPropInfo(id: number): PropInfo {
        return PropConfig.getProp(id);
    }

    /**
     * 使用道具
     */
    public useProp(propId: number) {
        let propCfg = PropConfig.getProp(propId);
        // 使用道具
        switch (propCfg.propType) {
            case PropType.ChangeFruit:
                // 加时卡
                break;
            case PropType.DelOneRuit:
                // 提示卡
                break;
            default:
                break;
        }

        this.setPropNum(propId, this.getPropNum(propId) - 1);
    }
}